/**
 * Project Name:nplus1
 * File Name:DictResource.java
 * Package Name:com.gamma.nplus1.modules.sys.api
 * Date:2017年5月19日上午11:14:49
 * Copyright (c) 2017, nplus1 Co., Ltd. All Rights Reserved.
 *
*/

package com.gamma.nplus1.modules.sys.api;

import javax.validation.ValidationException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.gamma.nplus1.common.constant.ControllerConstant;
import com.gamma.nplus1.common.mapper.JsonMapper;
import com.gamma.nplus1.common.persistence.Page;
import com.gamma.nplus1.common.utils.StringUtils;
import com.gamma.nplus1.common.web.BaseRestController;
import com.gamma.nplus1.modules.sys.entity.Dict;
import com.gamma.nplus1.modules.sys.service.DictService;

/**
 * ClassName:DictResource <br/>
 * Function: 字典api. <br/>
 * Date: 2017年5月19日 上午11:14:49 <br/>
 * 
 * @author jeno
 * @version
 * @since JDK 1.7
 */
@RestController
@RequestMapping(value = "/api/dict")
public class DictResource extends BaseRestController {

	@Autowired
	private DictService dictService;

	/**
	 * 
	 * detail:(get dict by id). <br/>
	 *
	 * @author jeno
	 * @param id
	 * @return
	 * @throws Exception
	 * @since JDK 1.7
	 */
	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	public ResponseEntity<Object> detail(@PathVariable String id) throws Exception {

		if (StringUtils.isBlank(id)) {
			throw new ValidationException("id is empty");
		}

		Dict dict = dictService.get(id);
		if (dict == null) {
			throw new ValidationException("dict is not found");
		}

		return super.buildSuccessResult(dict, String.format(ControllerConstant.SHOW, SysResourceConstant.DICT));
	}

	/**
	 * 
	 * detail:(get dict list). <br/>
	 * url例子:http://~/api/list?pageNo=1&sortby=id&order=desc&filter={"type","notice_type"}
	 * @author jeno
	 * @param id
	 * @return
	 * @throws Exception
	 * @since JDK 1.7
	 */
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public ResponseEntity<Object> list(@RequestParam String pageNo, @RequestParam(required = false) String sortby,
			@RequestParam(required = false) String order,@RequestParam(value = "filter", required = false) String filterStr) throws Exception {

		//排序
		String orderBy = super.buildOrderByStr(sortby, order);
		
		// 过滤器初始化
		Dict filter = (Dict) JsonMapper.fromJsonString(filterStr, Dict.class);

		if (filter == null) {
			filter = new Dict();
		}

		// query list
		Page<Dict> page = dictService.findPage(new Page<Dict>(pageNo, orderBy), filter);

		return super.buildSuccessResult(page, String.format(ControllerConstant.PAGE, SysResourceConstant.DICT));
	}

	/**
	 * 
	 * delete:(delete Dict). <br/>
	 *
	 * @author jeno
	 * @param id
	 * @return
	 * @throws Exception
	 * @since JDK 1.7
	 */
	@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
	public ResponseEntity<Object> delete(@PathVariable String id) throws Exception {
		Dict dict = new Dict();
		dict.setId(id);
		dictService.delete(dict);

		return super.buildSuccessResult(dict,String.format(ControllerConstant.DELETE, SysResourceConstant.DICT));
	}

	/**
	 * 
	 * create:(create dict). <br/>
	 *
	 * @author jeno
	 * @param dict
	 * @return
	 * @throws Exception
	 * @since JDK 1.7
	 */
	@RequestMapping(value = "/create", method = RequestMethod.POST,consumes=MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Object> create(@RequestBody Dict dict) throws Exception {
		
		//validate param
		String errorMsg = super.beanValidator(dict);
		
		if (StringUtils.isNoneEmpty(errorMsg)) {
			throw new ValidationException(errorMsg);
		}
		
		dictService.save(dict);
		
		return super.buildSuccessResult(dict,String.format(ControllerConstant.CREATE, SysResourceConstant.DICT));
	}
}
